package config

import (
	"fmt"
	"github.com/spf13/viper"
	"os"
)

func LoadConfigFromYaml() (v *viper.Viper, err error) {
	v = viper.New()
	v.SetConfigName("config.yaml")
	v.AddConfigPath("./config/")
	v.SetConfigType("yaml")
	v.Set("version", "2.22")  // 显示调用Set设置值
	v.SetDefault("V", "1.11") // 建立默认值

	// viper从环境变量读取
	v.SetEnvPrefix("spf")
	v.BindEnv("id")
	os.Setenv("spf_id", "111")

	if err = v.ReadInConfig(); err != nil {
		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
			// Config file not found; ignore error if desired
			fmt.Println("Config file not found; ignore error if desired")
		} else {
			// Config file was found but another error was produced
			fmt.Println("Config file was found but another error was produced")
		}
	}

	// 监控配置和重新获取配置
	
	return v, err
}
